import { H2oAd, H2oLifeTool, H2oUser,H2oHotEvent,H2oNotice,H2oGoods } from './bo';
import { QRecommendHot } from './httpcall';

class Repository{
  user:H2oUser| undefined
  hotOffset:number = 0
  lifePageMap:Map<string,string> | undefined
  verifyMap:Map<string,boolean> = new Map<string,boolean>()
  hotEvents: Array<string> = Array<string>()
  hotMaps:Map<string,H2oHotEvent> = new Map<string,H2oHotEvent>()
  lifeList: Array<H2oLifeTool> = new Array<H2oLifeTool>()
  noticeTagList:Array<string> = new Array<string>()
  worker:WechatMiniprogram.Worker | undefined
  constructor(){
    const user = wx.getStorageSync("user")
    if(user){
      try{
        if(user.jwt !== undefined && user.jwt !== ""){
          this.user = {jwt:user.jwt, name:user.name, avatar:user.avatar, role:user.role}
        }
      }catch(e){
        this.user = undefined
      }
    }
  }
  login(nick:string, avatar:string, jwt:string, role:string){
    if(this.user === undefined){
      this.user = new H2oUser()
    }
    this.user.name = nick
    this.user.avatar = avatar
    this.user.jwt = jwt
    this.user.role = role 
    wx.setStorageSync("user", this.user)
  };
  getUser():H2oUser|undefined{
    return this.user
  }
  getJwt():string{
    if(this.user !== undefined){
      return this.user.jwt
    }
    return ""
  }
  refreshJwt(jwt:string){
    if(this.user !== undefined){
      this.user.jwt = jwt
      wx.setStorageSync("user", this.user)
    }
  }
  getAd():Array<H2oAd>{
    const adList = new Array<H2oAd>();
    adList.push({adId:'1', img: '/assets/js.jpg', title:'非常好看的景色，大家都在看', loves:0, views:0})
    adList.push({adId:'2', img: '/assets/js.jpg', title:'非常好看的景色，大家都在看', loves:1, views:10})
    adList.push({adId:'3', img: '/assets/js.jpg', title:'非常好看的景色，大家都在看', loves:2, views:20})
    adList.push({adId:'4', img: '/assets/js.jpg', title:'非常好看的景色，大家都在看', loves:3, views:30})
    adList.push({adId:'5', img: '/assets/js.jpg', title:'非常好看的景色，大家都在看', loves:4, views:40})
    return adList
  }
  getLife():Array<H2oLifeTool>{
    const adList = this.lifeList;
    if(adList.length === 0){
      const lifePageMap = new Map<string,string>()
      adList.push({name:'Moments',title:'发瞬间', img:'wechat-moments'})
      adList.push({name:'Ad',title:'发公告', img:'notes'})
      adList.push({name:'XianWu',title:'换闲物', img:'send-gift'})
      adList.push({name:'AITravel',title:'AI旅游规划', img:'map-marked'})
      adList.push({name:'Book',title:'谈教材', img:'bookmark'})
      adList.push({name:'Col',title:'找服务', img:'friends'})
      adList.push({name:'Location',title:'附近玩', img:'location'})
      lifePageMap.set('Moments', '/pages/moment/index')
      lifePageMap.set('Ad', '/pages/notice/index')
      lifePageMap.set('AITravel', '/pages/aiplaint/index')
      this.verifyMap.set('Moments', true)
      this.verifyMap.set('Ad', true)
      this.verifyMap.set('XianWu', true)
      this.verifyMap.set('Book', true)
      this.lifePageMap = lifePageMap
    }
    return adList 
  }
  verifyPass(action:string):boolean{
    if(!this.verifyMap.has(action)){
      return true
    }
    return ((this.user !== undefined) && (this.user.jwt !== ""))
  }
  getLifeUrl(action:string):string|undefined{
    if(!this.verifyPass(action)){
      return "/pages/login/index"
    }
    if(this.lifePageMap !== undefined){
      return this.lifePageMap.get(action)
    }else{
      return undefined
    }
  }
  getGoodTools():Array<H2oLifeTool>{
    const adList = new Array<H2oLifeTool>();
    adList.push({name:'Password',title:'密码工具', img:'lock'})
    adList.push({name:'Bills',title:'记账', img:'records'})
    adList.push({name:'OKR',title:'定目标', img:'award'})
    adList.push({name:'Plan',title:'定计划', img:'clock'})
    adList.push({name:'CRM',title:'客户管理', img:'friends'})
    adList.push({name:'PanDian',title:'商品盘点', img:'goods-collect'})
    return adList 
  }
  recommendHot(beStart:boolean = false, cb:(res:Array<H2oHotEvent>)=>void){
    const elist = this.hotEvents
    const emap  = this.hotMaps
    if(beStart){
      this.hotOffset = 0
    }
    let pageSize = 40
    if(this.hotOffset >= elist.length){
      let page = Math.floor(this.hotOffset/pageSize + 1)
      QRecommendHot(page, pageSize, (data:any)=>{
        data.forEach((item: any)=>{
          if(!emap.has(item.id)){ 
            elist.push(item.id)
            emap.set(item.id, item)
          }
        })
        cb(this.getHotEvent(beStart))
      })
    }else{
      cb(this.getHotEvent(beStart))
    } 
  }
  getHotItem(id: string):H2oHotEvent|undefined{
    return this.hotMaps.get(id)
  }
  getHotEvent(beStart:boolean = false):Array<H2oHotEvent>{
    const elist = this.hotEvents
    const emap  = this.hotMaps
    const rList = new Array<H2oHotEvent>()
    if(beStart){
      this.hotOffset = 0
    }
    if(this.hotOffset >= elist.length){
      return rList 
    }
    let pageSize = 40
    let start = this.hotOffset
    let end = start + pageSize
    if(end > elist.length){
      end = elist.length
    }
    this.hotOffset = end
    for(let i = start; i < end; i++){
      const id = elist[i]
      const e  = emap.get(id)
      if(e !== undefined){
        rList.push(e)
      }
    }
    return rList 
  }
  getNotice():Array<H2oNotice>{
    const notList = new Array<H2oNotice>()
    notList.push({id:'1', img:'home-o', title:'房屋招租', tag:'租赁', content:'领包入住', author:'Simon',location:'深圳南山',      expired:"2023/11/30"})
    notList.push({id:'2', img:'home-o', title:'房屋求租', tag:'求租', content:'干净单间', author:'Simon',location:'深圳南山',
    expired:"2023/11/30"})
    notList.push({id:'3', img:'send-gift-o', title:'香港代购', tag:'代购', content:'香港代购奶粉', author:'Simon',location:'深圳南山',expired:"2023/11/30"})
    notList.push({id:'4', img:'friends-o', title:'跑腿', tag:'服务', content:'南山跑腿', author:'Simon',location:'深圳南山',
    expired:"2023/11/30"})
    notList.push({id:'5', img:'friends-o', title:'跑腿', tag:'服务', content:'南山跑腿', author:'Simon',location:'深圳南山',
    expired:"2023/11/30"})
    return notList
  }
  getNoticeList():Array<string>{
    if(this.noticeTagList.length == 0){
      this.noticeTagList.push('租赁')
      this.noticeTagList.push('求租')
      this.noticeTagList.push('代购')
      this.noticeTagList.push('跑腿')
      this.noticeTagList.push('服务')
    }
    return this.noticeTagList
  }
  getXianWu():Array<H2oGoods>{
    const goodList = new Array<H2oGoods>()
    let img = '/assets/vivo.png'
    goodList.push({id:'1', thumb:img, title:'vivo Y70s 5G版 /Y30 /Y30g', desc:'智能双摄大屏游戏vivo Y71 /Y66手机', author:'Simon',tag:'8成新',num:1, price:400, origin_price:500,currency:'¥'})
    goodList.push({id:'2', thumb:img, title:'vivo Y70s 5G版 /Y30 /Y30g', desc:'智能双摄大屏游戏vivo Y71 /Y66手机', author:'Simon',tag:'8成新',num:1, price:400, origin_price:500,currency:'¥'})
    goodList.push({id:'3', thumb:img, title:'vivo Y70s 5G版 /Y30 /Y30g', desc:'智能双摄大屏游戏vivo Y71 /Y66手机', author:'Simon',tag:'8成新',num:1, price:400, origin_price:500,currency:'¥'})
    goodList.push({id:'4', thumb:img, title:'vivo Y70s 5G版 /Y30 /Y30g', desc:'智能双摄大屏游戏vivo Y71 /Y66手机', author:'Simon',tag:'8成新',num:1, price:400, origin_price:500,currency:'¥'})
    goodList.push({id:'5', thumb:img, title:'vivo Y70s 5G版 /Y30 /Y30g', desc:'智能双摄大屏游戏vivo Y71 /Y66手机', author:'Simon',tag:'8成新',num:1, price:400, origin_price:500,currency:'¥'})
    goodList.push({id:'6', thumb:img, title:'vivo Y70s 5G版 /Y30 /Y30g', desc:'智能双摄大屏游戏vivo Y71 /Y66手机', author:'Simon',tag:'8成新',num:1, price:400, origin_price:500,currency:'¥'})
    return goodList
  }
}
export const dao = new Repository()